﻿using BCCServer.DAO.Interfaces;
using BCCServer.Model.Enums;
using BCCServer.Model.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BCCServer.DAO.chat
{
    public class chatDB:IchatDB
    {
        //protected static bccContext context = new Model.Models.bccContext();
       
        private readonly bccContext _context;

        public chatDB(bccContext Context)
        {
            _context = Context;
        }

        /// <summary>
        /// 根据账号查询聊天信息
        /// </summary>
        /// <param name="sendAccount"></param>
        /// <param name="receiveAccount"></param>
        /// <returns></returns>
        public  List<UChatinfo> selChatinfo(string sendAccount, string receiveAccount)
        {
            var chatInfos = _context.UChatinfos.Where(u => ((u.SendAccount == sendAccount && u.ReceiveAccount == receiveAccount) || u.SendAccount == receiveAccount && u.ReceiveAccount == sendAccount) && !u.IsDelete).AsNoTracking().ToList();

            return chatInfos;
        }

        /// <summary>
        /// 根据账号查询好友
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        public List<UFriend> selFriendsinfo(string account)
        {
            var friends = _context.UFriends.Where(u => u.Account==account&&u.Relationship== (int)Relationship.Friends && !u.IsDelete).AsNoTracking().ToList();

            return friends;
        }

        /// <summary>
        /// 根据账号查询未读消息
        /// </summary>
        /// <param name="sendAccount"></param>
        /// <returns></returns>
        public List<UChatinfo> selChatAllUnreadinfo(string receiveAccount)
        {
            var chatInfos = _context.UChatinfos.Where(u => u.ReceiveAccount == receiveAccount && !u.IsRead && !u.IsDelete).AsNoTracking().ToList();

            return chatInfos;
        }

        /// <summary>
        /// 添加聊天信息
        /// </summary>
        /// <param name="uChatinfo"></param>
        /// <returns></returns>
        public  int addChatinfo(UChatinfo uChatinfo)
        {
            _context.UChatinfos.Add(uChatinfo);
            return _context.SaveChanges();
        }
    }
}
